<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\FreightWay;
use App\Models\GoodsType;
use Dingo\Api\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class GoodsFreightWayController extends Controller
{
    protected $model;
    public function __construct(FreightWay $freightWay)
    {
        $this->model = $freightWay;
    }
    public function getGoodsFreight(Request $request)
    {
        $machineId = $request->machineId;
        $freightWay = $request->freightWay;
        $machine = DB::table('machines')->find($machineId);
        if(!$machine->connected_at){
            return $this->freig('机器离线');
        }
        if(!$machine->expire_time){
            return $this->xpir('设备过期');
        }
        $machine = DB::table('freight_ways')->where('machine_id', $machineId)->where('No',$freightWay)->where('stock', '>', 0)->where('state', 1)->first();
        $merchantId = DB::table('machine_managers')->select('manager_id')->where('machine_id',$machineId)->where('type',3)->first();
        if(is_null($merchantId)){
            $merchantId = DB::table('machine_managers')->select('manager_id')->where('machine_id',$machineId)->where('type',2)->first();
        }
        $operationId = DB::table('machine_managers')->select('manager_id')->where('machine_id',$machineId)->where('type',4)->first();
        if($machine){
            $goods = DB::table('goods')->where('id', $machine->goods_id)->first();
            //$domain = is_https().$_SERVER['HTTP_HOST'];
            $machine->merchantId = $merchantId?$merchantId->manager_id:0;
            $machine->operationId = $operationId?$operationId->manager_id:0;
            if($goods){
                $machine->goods = array(
                    'goodsId' => $goods->id,
                    'goodsName' => $goods->goods_name,
                    'goodsPrice' => $machine->price,
                    //'goodsImage' => $domain.$goods->goods_image,
                    'goodsImage' => ($goods->domainName).($goods->goods_image),
                    'merchantId' => $goods->merchant_id,
                    'machineId' => $machine->machine_id,
                    'goodsUnit' => goods_unit($goods->goods_unit),
                    'mainboradNo' => $machine->No
                );
            }else{
                $machine->goods = null;
            }
        }
        Log::info('goodinfos',[$machine]);
        return $this->success($machine);
    }
}       
        